package com.damai.utils;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
	  /**
     * 压缩文件
     * 
     * @param srcfile
     *            File[] 需要压缩的文件列表
     * @param zipfile
     *            File 压缩后的文件
	 * @throws IOException 
     */
    public static void compress(List<File> srcfile, java.io.File zipfile) throws IOException {
        byte[] buf = new byte[1024];
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipfile));
        for (File file : srcfile) {
            FileInputStream in = new FileInputStream(file);
            out.putNextEntry(new ZipEntry(file.getName()));
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
        out.close();
    }
    
    
    public static void compress(File destDir,File destFile) throws FileNotFoundException{
    	 new CompactAlgorithm(destFile).zipFiles(destDir);  
    }

   
    public static class CompactAlgorithm {  
    	  
        /** 
         * 完成的结果文件--输出的压缩文件 
         */  
        File targetFile;  
          
        public CompactAlgorithm() {}  
          
        public CompactAlgorithm(File target) {  
            targetFile = target;  
            if (targetFile.exists())  
                targetFile.delete();  
        }  
      
        /** 
         * 压缩文件 
         *  
         * @param srcfile 
         * @throws FileNotFoundException 
         */  
        public void zipFiles(File srcfile) throws FileNotFoundException {  
            ZipOutputStream out = null;  
            try {  
                out = new ZipOutputStream(new FileOutputStream(targetFile));
                if(srcfile.isFile()){  
                    zipFile(srcfile, out, "");  
                } else{  
                    File[] list = srcfile.listFiles();  
                    for (int i = 0; i < list.length; i++) {  
                        compress(list[i], out, "");  
                    }  
                }  
                  
                System.out.println("压缩完毕");  
            }finally {  
                try {  
                    if (out != null)  
                        out.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
      
        /** 
         * 压缩文件夹里的文件 
         * 起初不知道是文件还是文件夹--- 统一调用该方法 
         * @param file 
         * @param out 
         * @param basedir 
         */  
        private void compress(File file, ZipOutputStream out, String basedir) {  
            /* 判断是目录还是文件 */  
            if (file.isDirectory()) {  
                this.zipDirectory(file, out, basedir);  
            } else {  
                this.zipFile(file, out, basedir);  
            }  
        }  
      
        /** 
         * 压缩单个文件 
         *  
         * @param srcfile 
         */  
        public void zipFile(File srcfile, ZipOutputStream out, String basedir) {  
            if (!srcfile.exists())  
                return;  
      
            byte[] buf = new byte[1024];  
            FileInputStream in = null;  
      
            try {  
                int len;  
                in = new FileInputStream(srcfile);  
                out.putNextEntry(new ZipEntry(basedir + srcfile.getName()));  
      
                while ((len = in.read(buf)) > 0) {  
                    out.write(buf, 0, len);  
                }  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                    if (out != null)  
                        out.closeEntry();  
                    if (in != null)  
                        in.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
      
        /** 
         * 压缩文件夹 
         * @param dir 
         * @param out 
         * @param basedir 
         */  
        public void zipDirectory(File dir, ZipOutputStream out, String basedir) {  
            if (!dir.exists())  
                return;  
      
            File[] files = dir.listFiles();  
            for (int i = 0; i < files.length; i++) {  
                /* 递归 */  
                compress(files[i], out, basedir + dir.getName() + "/");  
            }  
        }  
      
          
        //测试  
        public static void main(String[] args) throws FileNotFoundException {  
            File f = new File("E:/Study/Java");  
            new CompactAlgorithm(new File( "D:/test",f.getName()+".zip")).zipFiles(f);  
        }  
      
    }  

}
